﻿using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Security;

using Bricks.Component.Utils;
using Bricks.Business.Models;
using Bricks.Business.Models.FlowProcess;
using Bricks.Site.Models;
using Bricks.Business.Data.Repositories.FlowProcess
;
using Bricks.Business.Service;


namespace ChiDao.Business.Service.Impl
{
    [Export(typeof(IFlowGraphNodeService))]
    internal class FlowGraphNodeService : IFlowGraphNodeService
    {
        [Import]
        protected IFlowGraphNodeRepository FlowGraphNodeRepository { get; set; }

        public List<FlowGraphNodeView> GetFlowGraphNode(string processID)
        {
            List<FlowGraphNodeView> flowGraphNodeViewList = new List<FlowGraphNodeView>();
            IQueryable<FlowGraphNode> flowGraphNodes = FlowGraphNodeRepository.Entities.Where(c => c.RecordID == processID);
            foreach (FlowGraphNode flowGraphNode in flowGraphNodes)
            {
                flowGraphNodeViewList.Add(new FlowGraphNodeView()
                {
                    ID = flowGraphNode.ID,
                    RecordID = flowGraphNode.RecordID,
                    PathStartNodeCode = flowGraphNode.PathStartNodeCode,
                    PathEndNodeCode = flowGraphNode.PathEndNodeCode,
                    OrderNo = flowGraphNode.OrderNo,
                    NodeType = flowGraphNode.NodeType,
                    ActionType = flowGraphNode.ActionType
                });
            }
            return flowGraphNodeViewList;
        }

        public void AddFlowGraphNode(List<FlowGraphNodeView> addList)
        {
            foreach (FlowGraphNodeView flowGraphNodeView in addList)
            {
                FlowGraphNode existFlowGraphNode = FlowGraphNodeRepository.Entities.SingleOrDefault(c => c.ID == flowGraphNodeView.ID);
                if (existFlowGraphNode == null)
                {
                    FlowGraphNode flowGraphNode = new FlowGraphNode()
                    {
                        ID = flowGraphNodeView.ID,
                        RecordID = flowGraphNodeView.RecordID,
                        PathStartNodeCode = flowGraphNodeView.PathStartNodeCode,
                        PathEndNodeCode = flowGraphNodeView.PathEndNodeCode,
                        OrderNo = flowGraphNodeView.OrderNo,
                        NodeType = flowGraphNodeView.NodeType,
                        ActionType = flowGraphNodeView.ActionType
                    };
                    FlowGraphNodeRepository.Insert(flowGraphNode);
                }
            }
        }

        public void UpdateFlowGraphNode(List<FlowGraphNodeView> updateList)
        {
            foreach (FlowGraphNodeView flowGraphNodeView in updateList)
            {
                FlowGraphNode flowGraphNode = FlowGraphNodeRepository.Entities.FirstOrDefault(m => m.ID == flowGraphNodeView.ID);
                if (flowGraphNode != null)
                {
                    flowGraphNode.RecordID = flowGraphNodeView.RecordID;
                    flowGraphNode.PathStartNodeCode = flowGraphNodeView.PathStartNodeCode;
                    flowGraphNode.PathEndNodeCode = flowGraphNodeView.PathEndNodeCode;
                    flowGraphNode.OrderNo = flowGraphNodeView.OrderNo;
                    flowGraphNode.NodeType = flowGraphNodeView.NodeType;
                    flowGraphNode.ActionType = flowGraphNodeView.ActionType; ;
                }
                FlowGraphNodeRepository.Update(flowGraphNode);
            }
        }
    }
}